﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.ParameterSettings
{
    public partial class ProjectDetails : BaseUI.BasePage
    {
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();
        private QS_PROJECTBLL projectBLL = new QS_PROJECTBLL();
        private QS_PROJECTDETAILBLL projectDetailBLL = new QS_PROJECTDETAILBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadLeftPage();
                LoadRightPage();
            }
        }

        /// <summary>
        /// 初始化右边页面
        /// </summary>
        private void LoadLeftPage()
        {
            var nodes = GetNodes();
            foreach (var item in nodes)
            {
                LEFT_THREE.Nodes.Add(item);
            }
        }

        /// <summary>
        /// 初始化左边页面
        /// </summary>
        private void LoadRightPage()
        {
        }

        private List<TreeNode> GetNodes(List<QS_PROJECT> entitys = null, decimal parentid = 0)
        {
            if (entitys == null)
            {
                int count;
                entitys = projectBLL.SelectByPage("CMY_ID IN (" + companyBLL.GetCompanyFatherAndChildByID(CurrentUserCompanyID) + ")", null, 0, 1000, out count);
            }
            var nodes = new List<TreeNode>();
            foreach (var item in entitys.Where(a => a.N_PARENTID == parentid))
            {
                var node = new TreeNode();
                node.Text = item.C_PRONAME;
                switch ((int)item.N_TYPE)
                {
                    case 0:
                        {
                            node.ImageUrl = "/Images/levelOnesmall.jpg";
                            node.ImageToolTip = "主节点";
                            var itemnodes = GetNodes(entitys, item.N_PROID);
                            foreach (var itm in itemnodes)
                            {
                                node.ChildNodes.Add(itm);
                            }
                        }
                        break;

                    case 1:
                        {
                            node.ImageUrl = "/Images/levelTwo.jpg";
                            node.ImageToolTip = "分节点";
                            var itemnodes = GetNodes(entitys, item.N_PROID);
                            foreach (var itm in itemnodes)
                            {
                                node.ChildNodes.Add(itm);
                            }
                        }
                        break;

                    case 2:
                        node.ImageUrl = "/Images/levelThr_01.jpg";
                        node.ImageToolTip = "子节点";
                        node.NavigateUrl = "javascript:ToReport(" + item.N_PROID + "," + item.N_TYPE + ",'" + item.C_PRONAME + "')";
                        break;

                    default:
                        break;
                }

                nodes.Add(node);
            }
            return nodes;
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (FROM_KEY.Value == "")
            {
                return;
            }
            var entity = new QS_PROJECTDETAIL();
            entity.PRO_NAME = FORM_NAME.Text.TrimEnd();
            entity.PRO_NUM = FORM_NUMBER.Text.Trim();
            if (entity.PRO_NUM == "")
            {
                MessageBox.Show(this, "详情编号不能为空");
                return;
            }
            if (projectDetailBLL.GetCount("SELECT COUNT(*) FROM QS_PROJECTDETAIL WHERE PRO_NUM = '" + entity.PRO_NUM + "'") > 0)
            {
                MessageBox.Show(this, "详情编号已存在不能重复添加");
                return;
            }
            entity.N_PROID = Convert.ToDecimal(FROM_KEY.Value);
            if (projectDetailBLL.Insert(entity))
            {
                FROM_DETAIL_KEY.Value = entity.DETAILID.ToString();
                MessageBox.Show(this, "添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show(this, "添加失败");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (FROM_KEY.Value == "")
            {
                return;
            }
            if (FROM_DETAIL_KEY.Value == "")
            {
                return;
            }

            var entity = new QS_PROJECTDETAIL();
            entity.DETAILID = Convert.ToDecimal(FROM_DETAIL_KEY.Value);
            entity.PRO_NAME = FORM_NAME.Text.TrimEnd();
            entity.PRO_NUM = FORM_NUMBER.Text.Trim();
            if (entity.PRO_NUM == "")
            {
                MessageBox.Show(this, "详情编号不能为空");
                return;
            }
            if (projectDetailBLL.GetCount("SELECT COUNT(*) FROM QS_PROJECTDETAIL WHERE PRO_NUM = '" + entity.PRO_NUM + "' AND DETAILID != " + entity.DETAILID) > 0)
            {
                MessageBox.Show(this, "详情编号已存在不能重复添加");
                return;
            }
            entity.N_PROID = Convert.ToDecimal(FROM_KEY.Value);
            if (projectDetailBLL.Update(entity))
            {
                MessageBox.Show(this, "修改成功");
                BindData();
            }
            else
            {
                MessageBox.Show(this, "修改失败");
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            FORM_NAME.Text = "";
            FORM_NUMBER.Text = "";
            BindData();
        }

        private void BindData()
        {
            if (FROM_KEY.Value == "")
            {
                return;
            }
            decimal id = Convert.ToDecimal(FROM_KEY.Value);
            var proEntity = projectBLL.Find(id);
            FORM_PRO_NAME.Text = proEntity.C_PRONAME;
            FORM_PRO_NUMBER.Text = proEntity.C_PRONUM;
            var sql = @"SELECT
	*
FROM
	QS_PROJECT T1
INNER JOIN
	QS_PROJECTDETAIL T2
ON
	T1.N_PROID = T2.N_PROID
WHERE
	T1.N_PROID = {0}";

            sql = string.Format(sql, id);
            if (ViewState["ORDER_COL"] != null)
            {
                sql += " ORDER BY " + (string)ViewState["ORDER_COL"] + " " + (string)ViewState["ORDER_SATA"];
            }
            var dt = projectBLL.Query(sql);
            GvProject.DataSource = dt;
            GvProject.DataBind();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvProject_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            if ((string)ViewState["ORDER_COL"] == sPage)
            {
                if ((string)ViewState["ORDER_SATA"] == "Desc")
                    ViewState["ORDER_SATA"] = "ASC";
                else
                    ViewState["ORDER_SATA"] = "Desc";
            }
            else
            {
                ViewState["ORDER_COL"] = e.SortExpression;
                ViewState["ORDER_SATA"] = "ASC";
            }
            BindData();
        }

        protected void GvProject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "cu=this.style.backgroundColor;this.style.backgroundColor='#d5e7ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=cu");
                e.Row.Attributes.Add("onclick", "BINGO(" + e.Row.Cells[0].Text + ")");
            }
        }

        protected void btnQuery_Click1(object sender, EventArgs e)
        {
            var id = Convert.ToDecimal(FROM_DETAIL_KEY.Value);
            var entity = projectDetailBLL.Find(id);
            FORM_NAME.Text = entity.PRO_NAME;
            FORM_NUMBER.Text = entity.PRO_NUM;
        }
    }
}